<?php
/** 
 * 权限规则
 *
 * @author   xg
 * @date     2019-09-18
 */

namespace app\auth\controller;

use app\base\controller\Backend;
use app\auth\model\AuthRuleModel;

class Rule extends Backend
{

    protected $noNeedLogin = [];

    /**
     * 规则列表
     * @param string  $keyword  搜索关键词
     * @param number  $is_menu  是否为菜单
     * @param number  $is_node  是否为节点
     * @param number  $page     当前页码
     * @param number  $limit    每页显示条数
     * @return array
     */
    public function index()
    {
        $rule = [
            'keyword'   => 'chsDash',
            'is_menu'   => 'number',
            'is_node'   => 'number',
            'page'      => 'number',
            'limit'     => 'number',
        ];

        $msg = [
            'keyword.chsDash'   => '搜索关键词格式错误',
            'is_menu.number'    => '菜单筛选格式错误',
            'is_node.number'    => '节点筛选格式错误',
            'page.number'       => '页码格式错误',
            'limit.number'      => '页数格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());

        if (!is_array($params)) {
            $this->error($params);
        }

        $rule_model = new AuthRuleModel();
        $rele_data  = $rule_model->getList($params);

        if (!empty($rule_model->getError())) {
            $this->error($rule_model->getError());
        }

        $this->success('成功', $rele_data);
    }

    /**
     * 规则添加
     * @param string  $pid      父ID
     * @param number  $path     规则路径
     * @param number  $route    前端路由
     * @param number  $title    标题
     * @param number  $icon     icon
     * @param number  $remark   备注
     * @param number  $is_menu  是否菜单
     * @param number  $is_node  是否节点
     * @param number  $weigh    权重
     * @param number  $status   状态
     * @return array
     */
    public function add()
    {
        $rule = [
            'pid'       => 'number',
            'path'      => 'max:100',
            'route'     => 'max:100',
            'title'     => 'require|max:50',
            'icon'      => 'max:50',
            'remark'    => 'max:255',
            'is_menu'   => 'between:0,1',
            'is_node'   => 'between:0,1',
            'weigh'     => 'number',
            'status'    => 'number',
        ];

        $msg = [
            'pid.number'            => '父ID格式错误',
            'path.max:100'          => '规则路径格式错误',
            'route.max:100'         => '前端路由格式错误',
            'title.require'         => '标题不能为空',
            'title.max:50'          => '标题格式错误',
            'icon.max:50'           => 'icon格式错误',
            'remark.max:255'        => '备注格式错误',
            'is_menu.between:0,1'   => '菜单筛选格式错误',
            'is_node.between:0,1'   => '节点筛选格式错误',
            'weigh.number'          => '权重格式错误',
            'status.number'         => '状态格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $rule_model = new AuthRuleModel();
        $rele_data  = $rule_model->addOne($params);

        if (!empty($rule_model->getError())) {
            $this->error($rule_model->getError());
        }

        $this->success('添加成功');
    }

    /**
     * 规则修改
     * @param string  $rule_id  规则ID
     * @param string  $pid      父ID
     * @param number  $path     规则路径
     * @param number  $route    前端路由
     * @param number  $title    标题
     * @param number  $icon     icon
     * @param number  $remark   备注
     * @param number  $is_menu  是否菜单
     * @param number  $is_node  是否节点
     * @param number  $weigh    权重
     * @param number  $status   状态
     * @return array
     */
    public function edit()
    {
        $rule = [
            'rule_id'   => 'require|number',
            'pid'       => 'number',
            'path'      => 'max:100',
            'route'     => 'max:100',
            'title'     => 'require|max:50',
            'icon'      => 'max:50',
            'remark'    => 'max:255',
            'is_menu'   => 'between:0,1',
            'is_node'   => 'between:0,1',
            'weigh'     => 'number',
            'status'    => 'number',
        ];

        $msg = [
            'rule_id.require'       => '缺少规则ID',
            'rule_id.number'        => '规则ID格式错误',
            'pid.number'            => '父ID格式错误',
            'path.max:100'          => '规则路径格式错误',
            'route.max:100'         => '前端路由格式错误',
            'title.require'         => '标题不能为空',
            'title.max:50'          => '标题格式错误',
            'icon.max:50'           => 'icon格式错误',
            'remark.max:255'        => '备注格式错误',
            'is_menu.between:0,1'   => '菜单筛选格式错误',
            'is_node.between:0,1'   => '节点筛选格式错误',
            'weigh.number'          => '权重格式错误',
            'status.number'         => '状态格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $rule_model = new AuthRuleModel();
        $rele_data  = $rule_model->editOne($params);

        if (!empty($rule_model->getError())) {
            $this->error($rule_model->getError());
        }

        $this->success('修改成功');
    }

    /**
     * 规则删除
     * @param string  $rule_id  规则ID
     * @return array
     */
    public function del()
    {
        $rule = [
            'rule_id'   => 'require|array',
        ];

        $msg = [
            'rule_id.require'   => '缺少规则ID',
            'rule_id.array'     => '规则ID格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $rule_model = new AuthRuleModel();
        $rele_data  = $rule_model->delOne($params);

        if (!empty($rule_model->getError())) {
            $this->error($rule_model->getError());
        }

        $this->success('删除成功');
    }

}